package ren.wxyz.algorithm.dsaj2.chap02;

/**
 * @author zxwang
 * @dateTime 2014年12月3日 下午7:39:54
 */
public class LowArray {

	private long[] a;
	
	public LowArray(int size) {
		this.a = new long[size];
	}
	
	public void setElem(int index, long value) {
		a[index] = value;
	}
	
	public long getElem(int index) {
		return a[index];
	}
	
	/**
	 * 启动方法
	 * @param args
	 */
	public static void main(String[] args) {
		LowArray arr = new LowArray(100);
		int nElems = 0;
		int j;
		
		// 插入10个项
		arr.setElem(0, 77);
		arr.setElem(1, 99);
		arr.setElem(2, 44);
		arr.setElem(3, 55);
		arr.setElem(4, 22);
		arr.setElem(5, 88);
		arr.setElem(6, 11);
		arr.setElem(7, 00);
		arr.setElem(8, 66);
		arr.setElem(9, 33);
		nElems = 10;
		
		for (j = 0; j < nElems; j++) {
			System.out.print(arr.getElem(j) + " ");
		}
		System.out.println();
		
		// 检索数据项
		int searchKey = 26;
		for (j = 0; j < nElems; j++) {
			if (arr.getElem(j) == searchKey) {
				break;
			}
		}
		if (j == nElems) {
			System.out.println("找不到 " + searchKey);
		}
		else {
			System.out.println("找到 " + searchKey);
		}
		
		// 删除数据项
		int deleteKey = 55;
		for (j = 0; j < nElems; j++) {
			if (arr.getElem(j) == deleteKey) {
				break;
			}
		}
		// 原算法在LowArray长度为10时，会数组越界
//		for (int k = j; k < nElems; k++) {
//			arr.setElem(k, arr.getElem(k + 1));
//		}
//		nElems--;
		nElems--;
		for (int k = j; k < nElems; k++) {
			arr.setElem(k, arr.getElem(k + 1));
		}
		
		// 展示
		for (j = 0; j < nElems; j++) {
			System.out.print(arr.getElem(j) + " ");
		}
		System.out.println();
	}
}
